package com.itboye.lottery.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.lottery.base.BaseActivity;
import com.itboye.lottery.R;
import com.itboye.lottery.adapter.NoticeDetailAdapter;
import com.itboye.lottery.adapter.ZqNoticeDetailAdapter;
import com.itboye.lottery.alert.ByAlert;
import com.itboye.lottery.bean.NoticeBean;
import com.itboye.lottery.dialog.CalendarDialog;
import com.itboye.lottery.enums.LotteryType;
import com.itboye.lottery.presenter.UserPresenter;
import com.itboye.lottery.retrofit.ResponseCallback;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

import static com.itboye.bylibrary.util.TimeUtils.parseServerTime;

public class Dlt_SsqPrizeDetailActivity extends BaseActivity implements ResponseCallback {

    RecyclerView rl_jczq_jclq_detail;
    LotteryType lotteryType;
    private String id;
    UserPresenter userPresenter = new UserPresenter(this);
    TextView item_tv_announcement_detail_title, item_tv_announcement_detail_qishu_time, tv_notice_detail_xiaoliang, tv_notice_detail_jiangchi;
    LinearLayout ll_announcement_detail_number;
    private NoticeDetailAdapter adapter;
    String ball_type, open_time;
    LinearLayout ll_number_detail_root;
    TextView tv_zqdetail_totalmatch, tv_notice_detail_title;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_dlt__ssq_prize_detail;
    }

    @Override
    protected void initData() {
        initLottryType();
    }

    private void initLottryType() {
        lotteryType = (LotteryType) getIntent().getSerializableExtra("type");
        id = getIntent().getStringExtra("id");
        ball_type = getIntent().getStringExtra("ball_type");
        open_time = getIntent().getStringExtra("open_time");
        rl_jczq_jclq_detail.setLayoutManager(new LinearLayoutManager(this));
        switch (lotteryType) {
            case SSQ:
            case DLT:
                tv_zqdetail_totalmatch.setVisibility(View.GONE);
                ll_number_detail_root.setVisibility(View.VISIBLE);
                setTitle("开奖详情");
                rightTextValue("历史开奖", view -> {
                    startActivity(new Intent(Dlt_SsqPrizeDetailActivity.this, HistoryPrizeActivity.class).putExtra("type", lotteryType));
                });
                userPresenter.getNoticeDetail(id);
                break;
            case JCLQ:
                ll_number_detail_root.setVisibility(View.GONE);
                setTitle("竞彩篮球");
                rightIconValue(R.mipmap.rili, view -> {
                    //选择日期查看公告
                    showTimerPicker();
                });
                getZqNoticeDetailData(open_time);
                break;
            case JCZQ:
                ll_number_detail_root.setVisibility(View.GONE);
                setTitle("竞彩足球");
                rightIconValue(R.mipmap.rili, view -> {
                    //选择日期查看公告
                    showTimerPicker();
                });
                getZqNoticeDetailData(open_time);
                break;
        }
//        rl_jczq_jclq_detail
    }

    private void getZqNoticeDetailData(String open_time) {
        tv_notice_detail_title.setText(open_time);
        userPresenter.getNoticeZqDetail(ball_type, parseServerTime(open_time, "yyyy-MM-dd") / 1000);
    }

    private void showTimerPicker() {
        CalendarDialog calendarDialog = new CalendarDialog(this, true);
        calendarDialog.setOnDateSelectedListener((year, month, day) -> {
            getZqNoticeDetailData(year + "-" + month + "-" + day);
        });
        calendarDialog.show();
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void success(@NotNull Object o, @NotNull String requestSuccessType) {
        if (requestSuccessType == userPresenter.getNoticeDetail_SUCCESS) {
            NoticeBean.NoticeItem noticeItem = (NoticeBean.NoticeItem) o;
            setDetailData(noticeItem);
        }
        if (requestSuccessType == userPresenter.getNoticeZqDetail_SUCCESS) {
            ArrayList<NoticeBean.NoticeItem> noticeItem = (ArrayList<NoticeBean.NoticeItem>) o;
            setDetailDataForZq(noticeItem);
        }
    }

    private void setDetailDataForZq(ArrayList<NoticeBean.NoticeItem> noticeItem) {
        tv_zqdetail_totalmatch.setText(Html.fromHtml("共<font color='red'>" + (noticeItem == null ? 0 : noticeItem.size()) + "</font>场比赛"));
        //足球adapter
        rl_jczq_jclq_detail.setAdapter(new ZqNoticeDetailAdapter(this, noticeItem));
    }

    private void setDetailData(NoticeBean.NoticeItem noticeItem) {
        item_tv_announcement_detail_title.setText(noticeItem.getName());
        item_tv_announcement_detail_qishu_time.setText("第" + noticeItem.getNumber() + "期 " + noticeItem.getOpen_time());
        tv_notice_detail_xiaoliang.setText(Html.fromHtml("本期销量(元)<br /><font color='red'>" + noticeItem.getSale_money() + "</font>"));
        tv_notice_detail_jiangchi.setText(Html.fromHtml("奖池金额(元)<br /><font color='red'>" + noticeItem.getAll_bonus() + "</font>"));
        ll_announcement_detail_number.removeAllViews();
        if (noticeItem.getRed_num() != null) {
            for (int i = 0; i < noticeItem.getRed_num().size(); i++) {
                ll_announcement_detail_number.addView(createNumBall(noticeItem.getRed_num().get(i), true));
            }
        }
        if (noticeItem.getBlue_num() != null) {
            for (int i = 0; i < noticeItem.getBlue_num().size(); i++) {
                ll_announcement_detail_number.addView(createNumBall(noticeItem.getRed_num().get(i), false));
            }
        }


        adapter = new NoticeDetailAdapter(this, noticeItem.getContent(), lotteryType);
        rl_jczq_jclq_detail.setAdapter(adapter);
    }


    private TextView createNumBall(String text, boolean isRed) {
        TextView textView = new TextView(this);
        textView.setText(text);
        textView.setGravity(Gravity.CENTER);
        textView.setTextColor(getResources().getColor(R.color.white));
        textView.setBackgroundDrawable(getResources().getDrawable(isRed ? R.drawable.solid_circle_border_red : R.drawable.solid_circle_border_blue));
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(4, 4, 0, 4);
        textView.setLayoutParams(layoutParams);
        return textView;
    }

    @Override
    public void fail(int code, @NotNull Object msg, @NotNull String requestErrorType) {
        ByAlert.alert(msg);
    }
}
